<?php

$DocRoot = $_SERVER['DOCUMENT_ROOT'];
include_once "$DocRoot/woflow/model/db.php";

class Entity 
{
	public function __construct($sCollection, $sIdAttr, $bReadOnly=true)
	{
		$this->_idAttr = $sIdAttr;
		$db = new Database($bReadOnly);
		$this->_db = $db->get();
		$this->_collection = $this->_db->selectCollection($sCollection);
	}

	private function select($select, $project, $bJson)
	{
		$result = $this->_collection->find($select, $project);
		$res = array();
		foreach ($result as $record) 
		{
			array_push($res, $record);
		}
		return ($bJson) ? json_encode($res) : $res;
	}

	public function get($sID, $bJson=false)
	{
		return $this->select(
				array("$this->_idAttr" => "$sID"), 
				array(), 
				$bJson
			);
	}

	public function getAll($aFilters, $aProjection, $bJson=false)
	{
		return $this->_collection->find($aFilters, $aProjection, $bJson);
	}

	protected $_db;
	protected $_collection;
	protected $_idAttr;
}



?>
